﻿/******************************************************    
文件名:TableStructurePropertyDB.cs
版权  (c)  2010-11-23 淘渺圈圈工作室
作者:黄媛媛
创建日期:2010-12-05
维护人员:黄媛媛
维护日期:2010-12-05
文件功能描述:金属材料热处理网络学习平台_访问TableStructureProperty数据表操作
******************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Data;

namespace Mmht.DB
{
    /// <summary>
    /// 组织和性能
    /// </summary>
    class TableStructurePropertyDB
    {
        operate op = new operate();

        public string UpdateStructureProperty(DataTable dt, int create_by)
        {
            clearAll();
            int success = 0;
            int failure = 0;
            string message_s = string.Empty;
            string message_f = "第";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                if (insertItem(row, create_by))
                {
                    success++;
                }
                else
                {
                    failure++;
                    message_f += i.ToString() + ",";
                }
            }
            message_s = "组织和性能数据表共更新数据" + success.ToString() + "条,共计更新失败" + failure.ToString() + "条。";
            if (failure > 0)
            {
                message_f = message_f.Substring(0, message_f.Length - 1) + "行数据更新失败。";
                return message_s + message_f;
            }
            else
            {
                return message_s;
            }
        }

        private bool clearAll()
        {
            string sql = @"DELETE FROM structure_property";
            return op.Execute(sql);
        }

        private bool deleteItem(string key)
        {
            string sql = @"DELETE FROM structure_property WHERE name='" + key + "'";
            return op.Execute(sql);
        }

        private bool insertItem(DataRow row, int create_by)
        {
            StructureProperty s = new StructureProperty();
            s.name = row[0].ToString();
            s.photo = "files/structure_property/" + row[1].ToString();
            s.description = row[2].ToString();
            s.size = row[3].ToString();
            s.strength = row[4].ToString();
            s.elongation = row[5].ToString();
            s.reduction = row[6].ToString();
            s.hardness = row[7].ToString();
            s.test = row[8].ToString();
            s.remark = row[9].ToString();
            s.create_by = create_by;
            string sql = @"INSERT INTO structure_property
             (name,photo,description,size,strength,elongation,reduction,
              hardness,test,remark,viewnum,created_at,created_by) VALUES
             ('"+s.name+"','"+s.photo+"','"+s.description+"','"+s.size+"','"+
                s.strength+"','"+s.elongation+"','"+s.reduction+"','"+s.hardness+"','"+
                s.test+"','"+s.remark+"','"+s.viewnum.ToString()+"','"+s.create_at+"','"+
                s.create_by.ToString()+"')";
            return op.Execute(sql);
        }

        private class StructureProperty
        {
            public string name,photo,description,size,strength,elongation,reduction,hardness,test,remark;
            public int create_by;
            public int viewnum = 0;
            public DateTime create_at = DateTime.Now;
        }
    }
}
